package server;
import fshandler.FileSystemHandler;
import org.apache.xmlrpc.server.PropertyHandlerMapping;
import org.apache.xmlrpc.server.XmlRpcServer;
import org.apache.xmlrpc.server.XmlRpcServerConfigImpl;
import org.apache.xmlrpc.webserver.WebServer;


public class JavaServer{

    public JavaServer ()
    {
        
    }

    public String sayHello(String name)
    {
        
        return new String("hello "+ name);
    }
    
    public static void main (String [] args) {
	try {
        	
		WebServer server = null;
		if(args != null)
		{	
			if(args[0] != null)
			{
				System.out.println(args[0]);			
				server = new WebServer((new Integer(args[0])).intValue());
			}
		}
		else
			server = new WebServer(3452);			
		System.out.println("Server Starting now.................");
		XmlRpcServer xmlRpcServer = server.getXmlRpcServer();
		PropertyHandlerMapping phm = new PropertyHandlerMapping();
		try
		{
			if( args != null)
			{
				if(args[1] != null)
					FileSystemHandler.setFileName(args[1]);
			}
			else
				FileSystemHandler.setFileName("properties.xml");
				
			FileSystemHandler.setupRpc();
			phm.addHandler("FileServer",FileSystemHandler.class);
			System.out.println("Success");
		}
		catch(Exception e)
		{
			System.out.println("Caught" + e);
		}
			
		xmlRpcServer.setHandlerMapping(phm);
		XmlRpcServerConfigImpl serverConfig = (XmlRpcServerConfigImpl) xmlRpcServer.getConfig();
	 	serverConfig.setEnabledForExtensions(true);
    		serverConfig.setContentLengthOptional(false);
		server.start();
                

        } catch (Exception exception) {
            System.err.println("JavaServer: " + exception.toString());
        }
    }
}
